package com.example.springbootmybatisdemo;

import com.alibaba.fastjson.JSON;
import com.example.springbootmybatisdemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Random;

/**
 * @author xm, huangwei_xm@163.com
 * @version 1.3
 * @description
 * @Copyright 2022- huangwei All rights reserved.
 * @since 2022.04.24
 */
@RestController
public class UserController {
    @Autowired
    UserMapper userMapper;

    @GetMapping(value = "operation/{type}/{id}")
    public void save(@PathVariable int type, @PathVariable int id) {
        int i = new Random().nextInt(100);
        UserPo userPo = new UserPo();
        userPo.setName("xm" + i);
        userPo.setAge(i);
        if (type == 1) {
            userMapper.save(userPo);
        }

        userPo.setId(id);
        userPo.setName("update_" + userPo.getName());
        if (type == 2) {
            userMapper.update(userPo);
        }

        userPo.setId(id);
        userPo.setName("delete_" + userPo.getName());
        if (type == 3) {
            userMapper.deleteById(userPo);
        }

        List<UserPo> data = userMapper.findAll();
        data.forEach(var -> {
            System.out.println(JSON.toJSONString(var));
        });
    }
}
